package expression.v1.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import expression.v1.model.ExpPkg;

@Repository(value = "expPkgDaoImp")
public class ExpPackageDaoImp extends BaseDao<ExpPkg> implements IExpPackageDao {

	@Override
	public int addExpPackage(ExpPkg pkg) {
		String sql = "insert into exp_pkg(id,keyword,pkgimg,createtime) values(?,?,?,?)";
		return this.getJdbcTemplate().update(sql, pkg.getId(),
				pkg.getKeyword(), pkg.getPkgImg(), new Date());
	}

	@Override
	public int addAndUpdateExpPackages(List<ExpPkg> pkgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteExpPackage(ExpPkg pkg) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteExpPackages(List<ExpPkg> pkgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ExpPkg queryExpPackage(String keyword) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ExpPkg> queryExpPackages(String... keyword) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ExpPkg> queryLatest(int from, int limit) {
		String sql = "select * from exp_pkg order by createtime desc limit ?,?";
		List<ExpPkg> expPkgs = this.getJdbcTemplate().query(sql,
				new Object[] { from, limit }, new RowMapper<ExpPkg>(){
					@Override
					public ExpPkg mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						ExpPkg expPkg = new ExpPkg();
						expPkg.setId(rs.getString("id"));
						expPkg.setKeyword(rs.getString("keyword"));
						expPkg.setPkgImg(rs.getString("pkgimg"));
						return expPkg;
					}});
		return expPkgs;
	}

	@Override
	public int countAll(){
		String sql = "select count(*) from exp_pkg";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class);
	}
	
	@Override
	public int countByConditions(Object...args){
		String sql = "select count(*) from exp_pkg where keyword=?";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class,args);
	}

	@Override
	public List<ExpPkg> queryCheckingSelKey(int state) {
		String sql = "select * from exp_pkg where querykey <> ? order by keyword asc";
		List<ExpPkg> expPkgs = this.getJdbcTemplate().query(sql,
				new Object[] { state }, new RowMapper<ExpPkg>(){
					@Override
					public ExpPkg mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						ExpPkg expPkg = new ExpPkg();
						expPkg.setId(rs.getString("id"));
						expPkg.setKeyword(rs.getString("keyword"));
						expPkg.setPkgImg(rs.getString("pkgimg"));
						return expPkg;
					}});
		return expPkgs;
	}
	
	@Override
	public List<ExpPkg> queryCheckedSelKey(int state) {
		String sql = "select * from exp_pkg where querykey = ? order by keyword asc";
		List<ExpPkg> expPkgs = this.getJdbcTemplate().query(sql,
				new Object[] { state }, new RowMapper<ExpPkg>(){
					@Override
					public ExpPkg mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						ExpPkg expPkg = new ExpPkg();
						expPkg.setId(rs.getString("id"));
						expPkg.setKeyword(rs.getString("keyword"));
						expPkg.setPkgImg(rs.getString("pkgimg"));
						return expPkg;
					}});
		return expPkgs;
	}

	@Override
	public int updateQueryKeyState(int state,String[] keywords) {
		StringBuilder sql = 
				new StringBuilder("update exp_pkg set querykey=? where keyword in (");
		for(int i=0;i<keywords.length;i++)
			sql.append("?,");
		sql.deleteCharAt(sql.length()-1);
		sql.append(")");
		Object[] args = new Object[keywords.length+1];
		int[] argsType = new int[args.length];
		args[0] = state;
		for(int i=1;i<args.length;i++)
			args[i] = keywords[i-1];
		argsType[0] = java.sql.Types.INTEGER;
		for(int i=1;i<args.length;i++)
			argsType[i] = java.sql.Types.VARCHAR;
		return this.getJdbcTemplate().update(sql.toString(), args, argsType);
	}
}
